﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;


namespace EECMISWS
{
    public static class HttpHelper
    {
        public static string Get(string urlString)
        {
            try 
            {
                //WebClient w = new WebClient();
                WebClientPro w = new WebClientPro(25000);
                Stream st = w.OpenRead(urlString);
                StreamReader sr = new StreamReader(st);
                string res = sr.ReadToEnd();
                sr.Close();
                st.Close();
                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        public static string Post(string url, string postData)
        {
            try
            {
                //WebClient w = new WebClient();
                WebClientPro w = new WebClientPro(25000);
                w.Headers[HttpRequestHeader.ContentType] = "application/json";
                w.Encoding = Encoding.UTF8;
                byte[] bRet = w.UploadData(url, "Post", Encoding.UTF8.GetBytes(postData));
                return Encoding.UTF8.GetString(bRet);
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        public static string PostForm(string url, string postData,string filePath)
        {
            try
            {
                //WebClient w = new WebClient();
                WebClientPro w = new WebClientPro(25000);
                w.Headers[HttpRequestHeader.ContentType] = "application/json";
                w.Headers.Add("info", postData);
                w.Encoding = Encoding.UTF8;
                byte[] bRet = w.UploadFile(url, filePath);
                return Encoding.UTF8.GetString(bRet);
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        public static void DownLoad(string url, string fileName)
        {
            try
            {
                //WebClient w = new WebClient();
                WebClientPro w = new WebClientPro(25000);
                w.Headers[HttpRequestHeader.ContentType] = "application/json";
                w.Encoding = Encoding.UTF8;
                w.DownloadFile(url, fileName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string Delete(string url, string postData)
        {
            WebClient w = new WebClient();
            w.Headers[HttpRequestHeader.ContentType] = "application/json";
            w.Encoding = Encoding.UTF8;
            byte[] bRet = w.UploadData(url, "DELETE", Encoding.UTF8.GetBytes(postData));
            return Encoding.UTF8.GetString(bRet);
        }
    }
}
